<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //push
let fruits = ['apple', 'banana'];
fruits.push('orange');  // 返回3，数组变为['apple','banana','orange']
console.log(fruits)
console.log(fruits.push('orange'))//4
console.log(fruits.push(''))//5

//pop
let lastItem = fruits.pop();  // 返回''，数组变回['apple','banana','orange','orange']
console.log(lastItem)

//shift
let firstItem = fruits.shift();  // 返回'apple'，数组变为['banana','orange','orange']
console.log(firstItem)
//unshift
fruits.unshift('grape');  // 返回4，数组变为['grape','banana','orange','orange']
console.log(fruits.unshift('grape'))//再次操作，返回5，['grape','grape','banana','orange','orange']
//map
let numbers = [1, 2, 3];
let doubled = numbers.map(x => x * 2);  // 返回[2,4,6]，原数组不变
//filter
let filtered = numbers.filter(x => x > 1);  // 返回[2,3]，原数组不变
console.log(filtered)
//return
let sum = numbers.reduce((total, num) => total + num, 0);  // 返回6
//some
let passed = numbers.some(x => x > 2);  // 返回true
//every
let allPassed = numbers.every(x => x > 0);  // 返回true
//find
let found = numbers.find(x => x > 2);  // 返回3
console.log(found)
//find与filter的区别
// 返回值类型
// find返回第一个符合条件的元素单个值若无匹配,则返回undefined
// filter返回所有符合条件的元素组成的新数组,无匹配时返回空数组[]
// 遍历行为
// find在找到第一个匹配项后立即停止遍历,具有短路特性
// filter会遍历整个数组收集所有匹配项

//forEach
numbers.forEach(num => console.log(num));  // 依次打印1,2,3



    </script>
</body>
</html>